<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\controller\OperationLog;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\Request;

class FileType extends Api
{
    protected $noNeedToken = [];
    protected $noNeedAuth = [];
    protected $noNeedLogin = [];

    /**
     * 获取附件类型列表
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function lists()
    {
        // 定义日志参数
        $options = array(
            'title' => '获取附件类型列表',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
            'token' => Request::instance()->header('token')
        );
        $fileTypeDatum = \app\api\model\FileType::all();
        OperationLog::OperSuccess([], $options, 'R', $fileTypeDatum);
        $this->success('success', ['code' => 200, 'data' => $fileTypeDatum]);
    }

    /**
     * 新增附件类型列表
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function add()
    {
        // 定义日志参数
        $options = array(
            'title' => '添加附件类型',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
            'token' => Request::instance()->header('token')
        );
        $param = Request::instance()->param() ?? [];
        $validRes = $this->validate($param, 'FileType.add');
        if ($validRes !== true) {
            OperationLog::OperError($param, $options, 'C', $validRes);
            $this->error('error', ['code' => 201, 'data' => $validRes]);
        }
        $checkExistFileType = \app\api\model\FileType::get(['name' => $param['name']]);
        if (empty($checkExistFileType)) {
            Db::startTrans();
            try {
                $newFileType = new \app\api\model\FileType();
                $newFileType->save([
                    'name' => $param['name']
                ]);
                Db::commit();
                OperationLog::OperSuccess($param, $options, 'C', '操作成功!');
                $this->success('success', ['code' => 200, 'data' => '操作成功!']);
            } catch (Exception $e) {
                Db::rollback();
                OperationLog::OperError($param, $options, 'C', $e->getMessage());
                $this->error('error', ['code' => 202, 'data' => $e->getMessage()], 202);
            }
        } else {
            OperationLog::OperError($param, $options, 'C', '该类别已存在!');
            $this->error('error', ['code' => 201, 'data' => '该类别已存在!']);
        }
    }

    /**
     * 获取附件类型详情
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail()
    {
        // 定义日志参数
        $options = array(
            'title' => '查询附件类型信息',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
            'token' => Request::instance()->header('token')
        );
        $param = Request::instance()->param() ?? [];
        $validRes = $this->validate($param, 'FileType.detail');
        if ($validRes !== true) {
            OperationLog::OperError($param, $options, 'R', $validRes);
            $this->error('error', ['code' => 201, 'data' => $validRes]);
        }
        $findFileTypeExist = \app\api\model\FileType::get(['id' => $param['id']])
            ->hidden(['update_time', 'create_time', 'delete_time']);
        if (empty($findFileTypeExist)) {
            OperationLog::OperError($param, $options, 'R', '类型信息不存在!');
            $this->error('error', ['code' => 201, 'data' => '类型信息不存在!']);
        } else {
            OperationLog::OperSuccess($param, $options, 'R', $findFileTypeExist);
            $this->success('success', ['code' => 200, 'data' => $findFileTypeExist]);
        }
    }

    /**
     * 附件类型编辑
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function edit()
    {
        // 定义日志参数
        $options = array(
            'title' => '编辑附件类型',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
            'token' => Request::instance()->header('token')
        );
        $param = Request::instance()->param() ?? [];
        $validRes = $this->validate($param, 'FileType.edit');
        if ($validRes !== true) {
            OperationLog::OperError($param, $options, 'U', $validRes);
            $this->error('error', ['code' => 201, 'data' => $validRes]);
        }
        $findFileType = \app\api\model\FileType::get(['id' => $param['id']]);
        if (empty($findFileType)) {
            OperationLog::OperError($param, $options, 'U', '附件类型不存在!');
            $this->error('error', ['code' => 201, 'data' => '附件类型不存在!']);
        } else {
            $checkFileTypeNameExist = \app\api\model\FileType::get(['name' => $param['name']]);
            if (!empty($checkFileTypeNameExist) && $checkFileTypeNameExist['id'] == $param['id']) {
                OperationLog::OperError($param, $options, 'U', '附件类型已存在!');
                $this->error('error', ['code' => 201, 'data' => '附件类型已存在!']);
            } else {
                Db::startTrans();
                try {
                    $findFileType->save($param, ['id' => $param['id']]);
                    Db::commit();
                    OperationLog::OperSuccess($param, $options, 'U', '操作成功!');
                    $this->success('success', ['code' => 200, 'data' => '操作成功!']);
                } catch (Exception $e) {
                    Db::rollback();
                    OperationLog::OperError($param, $options, 'U', $e->getMessage());
                    $this->error('error', ['code' => 202, 'data' => $e->getMessage()], '202');
                }
            }
        }
    }

    public function del()
    {
        // 定义日志参数
        $options = array(
            'title' => '删除附件类型',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
            'token' => Request::instance()->header('token')
        );
        $param = Request::instance()->param() ?? [];
        $validRes = $this->validate($param, 'FileType.del');
        if ($validRes !== true) {
            OperationLog::OperError($param, $options, 'D', $validRes);
            $this->error('error', ['code' => 201, 'data' => $validRes]);
        }
        $findFileType = \app\api\model\FileType::get(['id' => $param['id']]);
        if (empty($findFileType)) {
            OperationLog::OperError($param, $options, 'U', '附件类型不存在!');
            $this->error('error', ['code' => 201, 'data' => '附件类型不存在!']);
        } else {
            Db::startTrans();
            try {
                Db::name('file')->where('tid', '=', $param['id'])->update([
                    'tid' => 0
                ]);
                $findFileType->delete(true);
                Db::commit();
                OperationLog::OperSuccess($param, $options, 'D', '操作成功!');
                $this->success('success', ['code' => 200, 'data' => '操作成功!']);
            } catch (Exception $e) {
                Db::rollback();
                OperationLog::OperError($param, $options, 'D', $e->getMessage());
                $this->error('error', ['code' => 202, 'data' => $e->getMessage()], '202');
            }
        }
    }
}